﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConvertBase64
{
    public class Program
    {
        public static void Main()
        {
            //Read Notepad.exe
            byte[] buffer;
            using (FileStream fs = new FileStream("../../Resources/Notepad.exe", FileMode.Open))
            {
                buffer = new byte[fs.Length];
                fs.Read(buffer, 0, (int)fs.Length);
            }
            //Write notepad.txt in Base64
            using (StreamWriter sw = new StreamWriter("notepad.txt"))
            {
                string encodedText = Convert.ToBase64String(buffer);
                for (int i = 0; i < encodedText.Length; i++)
                {
                    if (i % 40 == 0 && i != 0)
                    {
                        sw.WriteLine();
                    }
                    sw.Write(encodedText[i]);
                }
            }

            //Read Base64 notepad.txt
            string byte64File;
            using (StreamReader sr = new StreamReader("notepad.txt"))
            {
                byte64File = sr.ReadToEnd();
            }
            //Create notepad.exe from the Base64 string.
            using (FileStream fs = new FileStream("notepad.exe", FileMode.Create))
            {
                byte[] bytes = Convert.FromBase64String(byte64File);
                fs.Write(bytes, 0, bytes.Length);
            }
        }
    }
}
